package com.blast.sspai.activity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import com.blast.sspai.R;
import com.blast.sspai.model.splash.Splash;

/**
 * Created   by IntelliJ IDEA
 * Project:  Sspai
 * Author:   marvin
 * Email:    mengxiangyu8@126.com
 * Date:     2015/6/16
 */


/**
 * 扉页
 */
public class SplashActivity extends Activity implements Runnable {

    private LinearLayout linear;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        linear = (LinearLayout) findViewById(R.id.splash_linear);

        // 加载 Animator
//        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.animator_change_color);
//        animator.setTarget(linear);
//        animator.start();
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_splash);
        linear.setAnimation(animation);
        // 创建线程并开始
        Thread thread = new Thread(this);
        thread.start();
    }

    /**
     * 执行线程
     */
    @Override
    public void run() {
        try {
            // 扉页等待 2.4 秒
            Thread.sleep(2400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        // 获取 SharedPreferences 找一下是否显示过引导教程
        SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE);

        // 获取 清单文件中的 versionCode 字段
        int versionCode = 1;
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        // 判断 是否显示过教程
        boolean isGuideShow = sharedPreferences.getBoolean(getPackageName() + versionCode, false);
        if (isGuideShow) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
            startActivity(intent);
            finish();
        }

      /*  //TODO 登陆注册页面还没有写，先直接执行MainActivity.class

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

        if (isGuideShow) {
            // TODO 如果显示过教程,则加载主页
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else {
            // 如果没显示过,则加载引导页
            Intent intent = new Intent(this, GuideActivity.class);
            startActivity(intent);
        }*/

        // finish
    }
}